<cfcomponent output="false">

	<!---- init ---->
	<cffunction name="init" returntype="any">
		<cfset INSTANCE = {} />
		<cfset INSTANCE.dao = createObject('component', 'UsersDAO').init() />
		<cfreturn this />
	</cffunction>

	<!---- getAll ---->
	<cffunction name="getAll" returntype="Users[]">
		<cfset var collection = [] />
		<cfset var obj = '' />
		<cfset var qry = '' />
		<cfset var i = 0 />
		<!---- get all records from database ---->
		<cfquery name="qry" datasource="ProdCat">
			SELECT * FROM Users
		</cfquery>
		<!---- load value objects ---->
		<cfloop from="1" to="#qry.recordcount#" index="i">
			<cfset obj = createObject('component', 'Users').init() />
			<cfset obj.setUserid(qry.Userid[i]) />
			<cfset obj.setLogin(qry.Login[i]) />
			<cfset obj.setPassword(qry.Password[i]) />
			<cfset arrayAppend(collection, obj) />
		</cfloop>
		<!---- return success ---->
		<cfreturn collection />
	</cffunction>
	
	<!---- getAll_paged ---->
	<cffunction name="getAll_paged" returntype="Users[]">
		<cfargument name="start" type="numeric" required="true" />
		<cfargument name="count" type="numeric" required="true" />
		<cfset var collection = [] />
		<cfset var obj = '' />
		<cfset var qry = '' />
		<cfset var i = 0 />
		<cfset var end=0 />
		<!---- get all records from database ---->
		<cfquery name="qry" datasource="ProdCat">
			SELECT * FROM Users			
		</cfquery>
		<!---- load value objects ---->
		<cfif (ARGUMENTS.start + ARGUMENTS.count - 1) GTE qry.recordcount >
			<cfset end =  qry.recordcount />
		<cfelse>
			<cfset end= ARGUMENTS.start + ARGUMENTS.count - 1 />
		</cfif>
		<cfloop from="#ARGUMENTS.start#" to="#end#" index="i">
			<cfset obj = createObject('component', 'Users').init() />
			<cfset obj.setUserid(qry.Userid[i]) />
			<cfset obj.setLogin(qry.Login[i]) />
			<cfset obj.setPassword(qry.Password[i]) />
			<cfset arrayAppend(collection, obj) />
		</cfloop>
		<!---- return success ---->
		<cfreturn collection />
	</cffunction>
	
	<!---- count ---->
	<cffunction name="count" returntype="numeric">
		<cfset var qry = "" />
		<cfquery name="qry" datasource="ProdCat">
			SELECT COUNT(UserID) AS total
			FROM Users
		</cfquery>
		<cfreturn qry.total[1] />
	</cffunction>

	<!--- check --->
	<cffunction name="CheckUserData" returntype="boolean" hint="Check user login and password">
		<cfargument name="Login" type="string" required="true" hint="User login">
		<cfargument name="Password" type="string" required="true" hint="User password">
		<cfset var qry = '' />
		<cfset var flag = "false" />
				
		<cfquery name="qry" datasource="ProdCat">
			SELECT *
			FROM Users
			WHERE Login = <cfqueryparam value="#ARGUMENTS.Login#" cfsqltype="cf_sql_varchar" null="false">
			AND Password = <cfqueryparam value="#ARGUMENTS.Password#" cfsqltype="cf_sql_varchar" null="false">
		</cfquery>
		
		<cfif qry.RecordCount IS NOT 0>
			<cfset flag="true">
		<cfelse>
			<cfset flag="false">
		</cfif>
				
		<cfreturn flag>

		<!---- return success ---->
		<cfreturn collection />		
	</cffunction>

</cfcomponent>
